using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wicresoft.OmsClient
{
    public partial class frmSetting : Form
    {
        public frmSetting()
        {
            InitializeComponent();
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {         
        }

        private void frmSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                this.Hide();
                e.Cancel = true;
            }
            catch (Exception ex)
            {
                Logger.Exception("frmSetting.frmSetting_FormClosing", ex);
            }
        }

        private delegate void CheckUserAsync(string url, string user, string pwd, string mobile, bool calendar, bool contact);


        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                string url = this.txtUrl.Text.Trim();
                string username = this.txtUsername.Text.Trim();
                string pwd = this.txtPwd.Text.Trim();
                string mobile = this.txtMoblie.Text.Trim();

                this.txtUrl.Enabled = false;
                this.txtUsername.Enabled = false;
                this.txtPwd.Enabled = false;
                this.txtMoblie.Enabled = false;
                this.cbCalendar.Enabled = false;
                this.cbContact.Enabled = false;
                this.btnOK.Enabled = false;
                this.btnCancel.Enabled = false;
                this.timerSaving.Enabled = true;
                CheckUserAsync cea = new CheckUserAsync(Check);
                cea.BeginInvoke(url, username, pwd, mobile,this.cbCalendar.Checked,this.cbContact.Checked, null, null);
            }
            catch (Exception ex)
            {
                Logger.Exception("frmSetting.btnOK_Click", ex);
                try
                {
                    StateLight.Red();
                    AfterCheck(true);
                }
                catch (Exception ex1)
                {
                    Logger.Exception("frmSetting.btnOK_Click.Hide", ex1);
                }
            }
        }

        private void Check(string url, string user, string pwd, string mobile, bool calendar, bool contact)
        {
            try
            {
                ConfigSaver.Save(url, user, pwd, mobile, calendar, contact);
                int result = UserService.CheckUser(user, pwd);
                bool flag = true;
                switch (result)
                {
                    case 1:
                        flag = true;
                        StateLight.Green();
                        UserService.IsAuthorized = true;
                        UserService.GetUserInfoFromOMS(user);
                        break;
                    case 0:
                        MessageBox.Show("You are unauthorized, please check your CIL, password or contact your administrator", "Warnning");
                        StateLight.Red();
                        UserService.IsAuthorized = false;
                        flag = false;
                        break;
                    default:
                        MessageBox.Show("Network Error", "Error");
                        StateLight.Gray();
                        UserService.IsAuthorized = false;
                        flag = false;
                        break;
                }
                
                AfterCheck(flag);
            }
            catch (Exception ex)
            {
                AfterCheck(true);
                Logger.Exception("frmSetting.Check", ex);

            }

        }

        private delegate void EnableAll(bool isclose);

        private void AfterCheck(bool isclose)
        {
            if (this.InvokeRequired)
                this.Invoke(new EnableAll(AfterCheck), isclose);
            else
            {
                this.timerSaving.Enabled = false;
                this.Text = "Setting";
                this.txtUrl.Enabled = true;
                this.txtUsername.Enabled = true;
                this.txtPwd.Enabled = true;
                this.txtMoblie.Enabled = true;
                this.cbCalendar.Enabled = true;
                this.cbContact.Enabled = true;
                this.btnOK.Enabled = true;
                this.btnCancel.Enabled = true;
                if (isclose)
                    this.Hide();
            }
            
        }

        private void timerSaving_Tick(object sender, EventArgs e)
        {
            string title = this.Text;
            if (title.Length == 7)
                title = "Setting Saving";
            else if (title.Length == 14)
                title = "Setting Saving.";
            else if (title.Length == 15)
                title = "Setting Saving..";
            else if (title.Length == 16)
                title = "Setting Saving...";
            else if (title.Length == 17)
                title = "Setting Saving....";
            else if (title.Length == 18)
                title = "Setting Saving.....";
            else if (title.Length == 19)
                title = "Setting Saving......";
            else if (title.Length == 20)
                title = "Setting Saving";
            this.Text = title;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Hide();
            }
            catch (Exception ex)
            {
                Logger.Exception("frmSetting.btnCancel_Click", ex);
            }
        }

        private void CheckInput()
        {
            if (this.txtUsername.Text.Trim() == "" || this.txtPwd.Text.Trim() == "" || this.txtMoblie.Text.Trim() == "" || this.txtUrl.Text.Trim() == "")
            {
                this.btnOK.Enabled = false;
            }
            else
                this.btnOK.Enabled = true;
        }

        private void txtUrl_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        private void txtEmail_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        private void txtMoblie_TextChanged(object sender, EventArgs e)
        {
            CheckInput();
        }

        public void ShowWindow()
        {
            this.Show();
            txtUrl.Text = SyncConfigManage.ProviderUrl;
            txtUsername.Text = SyncConfigManage.UserName;
            txtPwd.Text = SyncConfigManage.Password;
            txtMoblie.Text = SyncConfigManage.Mobile;
            cbCalendar.Checked = SyncConfigManage.EnableCalendarSync.ToLower() == "true" ? true : false;
            cbContact.Checked = SyncConfigManage.EnableContactSync.ToLower() == "true" ? true : false;
        }


    }
}